class @AdminBookCreateView extends CustomView

    el: $('#content')

    initialize: ->
       super

    render: ->
        model = @model
        el = $(@el)

        @GetTemplate(SiteMap.Admin.Books.CreateTemplate, (compiledTemplate) ->
            el.html(compiledTemplate(model.toJSON()))
            
            $("#UserId").combobox({
                source: SiteMap.Admin.Shared.GetUsers,
                isDynamic:true
            })
            $("#Authors").combobox({
                isMulti: true
                isDynamic: true
                source: SiteMap.Admin.Shared.GetAuthors
            })
            $("#Categories").combobox({ 
                isMulti: true 
                isDynamic: true
                source: SiteMap.Admin.Shared.GetCategories
            })
            $("#DateOfPublish").datepicker()
            $("#Avatar").ImageUpload()

            $("#FileName").FileUploader()
        )

    events:
        "click .buttons>button.save": "save"
        "click .buttons>button.cancel": "cancel"

    save: (e) ->
        e.preventDefault()
        if !@IsValid()
            Messages.Clear()
            Messages.WriteMessage('Please, correct errors.')
            return

        Loading.Start()
        Messages.WriteMessage('Creating book.')
        data = $(@el).find("form.general").serialize()

        $.ajax(
            url: SiteMap.Admin.Books.Create
            type: 'POST'
            data: data
            success: (response) =>
                if response.SuccessMessage? && response.SuccessMessage.length
                    Messages.WriteSuccessMessage(response.SuccessMessage)
                    @Navigate(SiteMap.Admin.Books.Listing, true)
                else
                    Messages.WriteErrors(response.Errors)
                    Loading.Stop()
        )

    cancel: (e) ->
        e.preventDefault()
        Loading.Start()
        Messages.Clear()
        @Navigate(SiteMap.Admin.Books.Listing, true)
